/* Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.xelnaga.defiler.web.page.mapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.riotfamily.common.web.cache.ServletUtils;
import org.xelnaga.defiler.domain.HtmlPageModel;

/**
 * @author Felix Gnass [fgnass at neteye dot de]
 * @since 6.5
 */
public class PageFacade {
	protected HttpServletRequest request;
	
	protected HttpServletResponse response;

	private HtmlPageModel page;

	public PageFacade(HtmlPageModel page, HttpServletRequest request) {
		this(page, request, null);
	}
	
	public PageFacade(HtmlPageModel page, HttpServletRequest request,
			HttpServletResponse response) {
		
		this.request = request;
		this.response = response;
		this.page = page;
	}
	
	public HtmlPageModel getPage() {
		return page;
	}

	public boolean isRequestable() {
		return (page.getLive() != null && page.getLive()
				&& page.getSite().getEnabled() != null 
				&& page.getSite().getEnabled());
	}
	
	public String getRelativeUrl() {
		return getRelativeUrl(null, true);
	}

	public String getRelativeUrl(String suffix) {
		return getRelativeUrl(suffix, true);
	}
	
	public String getRelativeUrl(String suffix, boolean encode) {
		StringBuilder url = new StringBuilder();
		url.append(page.getPath());
		if (suffix == null) {
//			suffix = page.getSite().getDefaultSuffix(page);
			suffix = "/";
		}
		url.append(suffix);
		if (encode && response != null) {
			return ServletUtils.resolveAndEncodeUrl(url.toString(), request, response);
		}
		return ServletUtils.resolveUrl(url.toString(), request);
	}
	
	public String getUrl() {
		return getUrl(null);
	}

	public String getUrl(String suffix) {
		return getUrl(suffix, true);
	}
	
	public String getUrl(String suffix, boolean encode) {
		if (!page.getSite().hostNameMatches(request.getServerName())) {
			return getAbsoluteUrl(suffix);
		}
		return getRelativeUrl(suffix, encode);
	}
	
	public String getAbsoluteUrl() {
		return getAbsoluteUrl(null);
	}

	public String getAbsoluteUrl(String suffix) {
		return getAbsoluteUrl(suffix, true);
	}

	public String getAbsoluteUrl(String suffix, boolean encode) {
		return makeAbsolute(page.getSite().getHostName(), request.isSecure(), 
				ServletUtils.getServerNameAndPort(request),
				request.getContextPath(), getRelativeUrl(suffix, encode));
	}
	
	public String makeAbsolute(String hostName, boolean secure, String defaultHost, 
			String contextPath, String path) {
		
		StringBuffer url = new StringBuffer();
		
		if (hostName != null || defaultHost != null) {
			url.append(secure
					? ServletUtils.SCHEME_HTTPS 
					: ServletUtils.SCHEME_HTTP);
			
	        url.append("://");
	        if (hostName != null) {
				url.append(hostName);
			}
			else {
				url.append(defaultHost);
			}
		}
        
        if (contextPath.length() > 0 && !path.startsWith(contextPath)) {
        	url.append(contextPath);
        }
		url.append(path);
		return url.toString();
	}
	public String getSecureUrl() {
		return getSecureUrl(null);
	}

	public String getSecureUrl(String suffix) {
		return getSecureUrl(suffix, true);
	}

	public String getSecureUrl(String suffix, boolean encode) {
		if (request.isSecure() && request.getServerName().equals(
				page.getSite().getHostName())) {
			
			return getUrl(suffix);
		}
		return makeAbsolute(page.getSite().getHostName(), true, 
				ServletUtils.getServerNameAndPort(request), 
				request.getContextPath(), getRelativeUrl(suffix, encode));
	}
	
//	public Collection<Page> getAncestors() {
//		LinkedList<Page> pages = Generics.newLinkedList();
//		HtmlPageModel page = this.page;
//		while (page != null) {
//			pages.addFirst(page);
//			page = page.getParent();
//		}
//		return pages;
//	}
		
//	public List<PageFacade> getChildren() {
//		CacheTagUtils.tagIfSupported(page);
//		VirtualPageType type = page.getSite().getSchema().getVirtualChildType(page);
//		if (type != null) {
//			Collection<Page> children = type.listChildren(page);
//			CacheTagUtils.tagIfSupported(children);
//			return createFacades(children);
//		}
//		return createFacades(page.getChildren());
//	}

//	public List<PageFacade> getSiblings() {
//		Page parent = page.getParent();
//		if (parent == null) {
//			return Collections.singletonList(this);
//		}
//		return new PageFacade(parent, request, response).getChildren();
//	}
	
//	public PageFacade getPreviousSibling() {
//		List<PageFacade> siblings = getSiblings();
//		int i = siblings.indexOf(this);
//		if (i > 0) {
//			return siblings.get(i - 1);
//		}
//		return null;
//	}
	
//	public PageFacade getNextSibling() {
//		List<PageFacade> siblings = getSiblings();
//		int i = siblings.indexOf(this);
//		if (i < siblings.size() - 1) {
//			return siblings.get(i + 1);
//		}
//		return null;
//	}
	
//	protected List<PageFacade> createFacades(Collection<? extends Page> pages) {
//		ArrayList<PageFacade> result = Generics.newArrayList();
//		for (Page page : pages) {
//			if (page.getContentContainer().getLiveVersion() != null || isPreview(page)) {
//				result.add(new PageFacade(page, request, response));
//			}
//		}
//		return result;
//	}
	
}
